﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.IO;

namespace ClearUnit.Tests
{
    public static class SampleTestsFactory
    {
        static string SampleAssemblyPath
        {
            get
            {
                var relativePath = @"..\..\..\SampleTests\bin\Debug\ClearUnit.Tests.SampleTests.dll";
                return Path.GetFullPath(relativePath);
            }
        }

        static Assembly _sampleAssembly;

        public static Assembly SampleAssembly
        {
            get
            {
                if (_sampleAssembly == null)
                    LoadSampleTestsAssemlby();

                return _sampleAssembly;
            }
        }

        [MethodImpl(MethodImplOptions.Synchronized)]
        static void LoadSampleTestsAssemlby()
        {
            if (_sampleAssembly != null)
                return;

            _sampleAssembly = Assembly.LoadFile(SampleAssemblyPath);
        }

        public static MethodInfo GetMethod(string typeName, string methodName)
        {
            var type = GetType(typeName);
            return GetMethod(type, methodName);
        }

        public static MethodInfo GetMethod(Type type, string methodName)
        {
            return type.GetMethod(methodName, BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public);
        }

        public static Type GetType(string typeName)
        {
            return SampleAssembly.GetTypes().Single(t => t.Name == typeName);
        }
    }
}
